from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from math import *
from time import *
from glturtle import *
import colors
print dir(colors)


class Clock:
    SECONDS_IN_HOUR = 3600 # constant in seconds
    # glyphs for drawing digits by turtle for digital clock
    figures = {\
                    '0': '_ c1 d d r u u l ^ r r',\
                    '1': '_ c1 d d ^ r u u',\
                    '2': '_ c1 r d l d r ^ r u u',\
                    '3': '_ c1 r d l r d l ^ r r u u',\
                    '4': '_ c1 d r d u u ^ r',\
                    '5': 'r _ c1 l d r d l ^ r r u u',\
                    '6': 'r  _ c1 l d d r u l ^ r u r',\
                    '7': '_ c1 r d d ^ r u u',\
                    '8': '_ c1 d d r u l r u l ^ r r',\
                    '9': 'd d _ c1 r u u l d r ^ u r',\
                    ':': 'c2 s < < < d d _ . ^ d d d d d d d d d d d _ . ^ b > > > r',\
                    '.': 's d d < < < _ . ^ > > > b r'
                    }

    default_palette = {\
                        'ticks': (1, 0, 0),\
                        'digits': (0, 1, 0),\
                        'square': (1, 1, 1),\
                        'h-arrow': (0, 1, 0),\
                        'm-arrow': (0, 1, 0),\
                        'seconds': (1, 1, 1),\
                        'semicolon': (1, .5, .5)}


    sysColors = dict([(c.upper(), getattr(colors, c)) for c in dir(colors) if type(getattr(colors, c)) is tuple])

    def __init__(self, time_shift_hour, scale, x, y, is_analog_clock, palette):
        #def stricmp(s1, s2): # case insensitive comparison
        self.time_shift_sec = time_shift_hour * Clock.SECONDS_IN_HOUR
        self.scale = scale
        self.x = x
        self.y = y
        self.is_analog_clock = is_analog_clock
        self.palette = palette.copy()

        #print 'instance', Clock.sysColors
        for i in self.palette.keys():
            while type(self.palette[i]) is not tuple:
                if type(self.palette[i]) is str:
                    if self.palette.has_key(self.palette[i]): # reference to another key
                        self.palette[i] = self.palette[self.palette[i]]
                    else:
                        self.palette[i] = Clock.default_palette[i]

    def draw(self):
        def tick(a):
            a = pi*a/180.
            r1 = 0.8
            a1 = a
            r2 = 0.9
            a2 = a
            x1 = r1*cos(a1)
            y1 = r1*sin(a1)
            x2 = r2*cos(a2)
            y2 = r2*sin(a2)

            glColor3f(self.palette['ticks'][0],self.palette['ticks'][1],self.palette['ticks'][2])
            glBegin(GL_LINES)
            glVertex3f(x1, y1, 0)
            glVertex3f(x2, y2, 0)
            glEnd()

        def polR2cart(p):
            r = p[0]
            a = p[1]
            x = r * cos(a)
            y = r * sin(a)
            return (x, y)

        def lineC(c1, c2): # line in Cartesian system
            glLineWidth(2)
            glBegin(GL_LINES)
            glVertex3f(c1[0], c1[1], 0)
            glVertex3f(c2[0], c2[1], 0)
            glEnd()

        def linePR(p1, p2): # line in Polar system
            c1 = polR2cart(p1)
            c2 = polR2cart(p2)
            lineC(c1, c2)

        def pointC(c):
            glPointSize(3)
            glBegin(GL_POINTS)
            glVertex3f(c[0], c[1], 0)
            glEnd()

        def pointPR(p):
            c = polR2cart(p)
            pointC(c)

        def rad2deg(a):
            return 180.*a/pi

        def deg2rad(a):
            return pi*a/180.

        def arrowR(r, a):
            d1 = 6.
            d2 = 3.5
            t1 = (r, a)
            t2 = (r/d1, a - pi)
            t3 = (r/d2, a - deg2rad(150))
            t4 = (r/d2, a + deg2rad(150))
            linePR(t1, t3)
            linePR(t3, t2)
            linePR(t2, t4)
            linePR(t4, t1)

        def draw_analog_clock(fhour, minutes, seconds):
            #print 'draw analog clock'
            a1 = ((-pi/30 * minutes) + pi/2)
            a3 = (-pi/6 * fhour) + pi/2
            a4 = (-pi/30 * seconds) + pi/2

            for i in range(0, 360, 30):
                tick(i)

            ang = int(rad2deg(a4))
            for i in range(90, ang, -6):
                a = deg2rad(i)
                glColor3f(self.palette['seconds'][0], self.palette['seconds'][1], self.palette['seconds'][2])
                pointPR((0.8, a))

            glColor3f(self.palette['m-arrow'][0], self.palette['m-arrow'][1], self.palette['m-arrow'][2])
            arrowR(0.8, a1)
            glColor3f(self.palette['h-arrow'][0], self.palette['h-arrow'][1], self.palette['h-arrow'][2])
            arrowR(0.5, a3)
            glColor3f(0, 1, 1)

        def draw_digital_clock():
            tur.c1 = self.palette['digits']
            tur.c2 = self.palette['semicolon']
            tur.draw(Clock.figures[hh1])
            tur.draw(Clock.figures[hh2])
            tur.draw(Clock.figures[':'])
            tur.draw(Clock.figures[mm1])
            tur.draw(Clock.figures[mm2])
            #print 'draw digital clock'


        glPushMatrix()
        glTranslate(self.x, self.y, 0)
        glScale(self.scale, self.scale, 1)

        # calculate time ( hour and minute)
        time_one = time() + self.time_shift_sec
        my_time = localtime(time_one)
        hour = my_time[3]
        minutes = my_time[4]
        seconds = my_time[5]

        h1 = (hour/10)%10
        h2 = hour%10
        m1 = (minutes/10)%10
        m2 = minutes%10
        hh1 = str(h1)
        hh2 = str(h2)
        mm1 = str(m1)
        mm2 = str(m2)
        fhour = hour + ((1/60.) * minutes)

        tur = Turtle(-.8, .1, .2, .2) # cx=0, cy=0, scaleX=1, scaleY=1

        #choosing the type of clock
        if self.is_analog_clock:
           draw_analog_clock(fhour, minutes, seconds)
        else:
            draw_digital_clock()

         #draw square around clock
        glColor3f(self.palette['square'][0], self.palette['square'][1], self.palette['square'][2])
        glBegin(GL_LINES)
        glVertex3f(-.9, .9, 0)
        glVertex3f(.9, .9, 0)
        glVertex3f(.9, .9, 0)
        glVertex3f(.9, -.9, 0)
        glVertex3f(.9, -.9, 0)
        glVertex3f(-.9, -.9, 0)
        glVertex3f(-.9, -.9, 0)
        glVertex3f(-.9, .9, 0)
        glEnd()

        glPopMatrix()

print 'bbbbbbb'